package pers.qianyu.model.vo;

import cn.hutool.core.bean.*;
import cn.hutool.core.bean.copier.*;
import lombok.*;
import pers.qianyu.model.entity.*;

import java.util.*;

@Data
public class UserVO {
    private Long id;
    private String username;
    private String[] roles;

    public static UserVO fromUserPO(UserPO userPO) {
        return new Converter().convertToVO(userPO);
    }

    private static class Converter implements IConverter<UserPO, UserVO> {
        @Override
        public UserVO convertToVO(UserPO userPO) {
            UserVO vo = new UserVO();
            BeanUtil.copyProperties(userPO, vo, CopyOptions.create().ignoreError());
            vo.roles = Optional.ofNullable(userPO.getRoles())
                    .map(roles -> roles.split(","))
                    .orElse(new String[0]);
            return vo;
        }
    }
}
